package com.haisen.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.haisen.core.utils.InsertBatch;
import com.haisen.core.utils.MyMapper;
import com.haisen.core.utils.PageResultSet;
import com.haisen.dataobject.entity.BussItem;
import com.haisen.dataobject.entity.StationBase;
import com.haisen.dataobject.entity.VerifyCodeHist;
import com.haisen.dataobject.mapper.base.BussItemMapper;
import com.haisen.dataobject.mapper.base.StationBaseMapper;
import com.haisen.dataobject.mapper.base.VerifyCodeHistMapper;
import com.haisen.dataobject.query.StationBaseQuery;
import com.haisen.dataobject.query.VerifyCodeHistQuery;
import com.haisen.service.StationBaseService;
import com.haisen.service.VerifyCodeHistService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.weekend.Weekend;
import tk.mybatis.mapper.weekend.WeekendCriteria;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class StationBaseServiceImpl implements StationBaseService{

    @Autowired
   private StationBaseMapper stationBaseMapper;


    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    private InsertBatch<StationBase> batchAdd = null;

    @Override
    public void create(StationBase stationBase) {
        stationBaseMapper.insertSelective(stationBase);
    }

    @Override
    public void insertBatch(List<StationBase> stationBaseList) {
        SqlSession sqlSession= sqlSessionFactory.openSession(ExecutorType.BATCH);
        MyMapper<StationBase> myMapper = (MyMapper<StationBase>) sqlSession.getMapper(StationBaseMapper.class);
        batchAdd = new InsertBatch<>();
        batchAdd.insertBatch(sqlSession,myMapper,stationBaseList);

    }

    @Override
    public PageResultSet<StationBase> findByPage(StationBaseQuery stationBaseQuery) {

        if (!StringUtils.isEmpty(stationBaseQuery.getOrderBy())){
            PageHelper.orderBy(stationBaseQuery.getOrderBy());
        }
        PageHelper.offsetPage(stationBaseQuery.getOffset(),stationBaseQuery.getLimit());

        Weekend<StationBase> example = Weekend.of(StationBase.class);
        WeekendCriteria<StationBase,Object> criteria = example.weekendCriteria();

        if (!StringUtils.isEmpty(stationBaseQuery.getPhone())){
            criteria.andEqualTo(StationBase :: getPhone,stationBaseQuery.getPhone());
        }

        example.setOrderByClause("id desc");

        PageResultSet<StationBase> resultSet = new PageResultSet<>();
        resultSet.setRows(stationBaseMapper.selectByExample(example));
        resultSet.setTotal(stationBaseMapper.selectCountByExample(example));
        return resultSet;
    }
}
